﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;

namespace UI
{
    public class NPCUIBuildingItem : MonoBehaviour
    {
        public UILabel buildingLevel;
        public UILabel buildingName;
        public UISprite building;
        public UILabel additionType;
        public UILabel addition;
        public UILabel cost;
        public UISprite cdMask;
        int type;//// 0:战车 1：城门 2：塔
        int maxLevel;
        int curLevel;
        int costNum;
        NPCUIBuildingPage page;

        const string saoChariot = "UI_jianzhu_01_c";
        const string aloChariot = "UI_jianzhu_02_c";
        const string saoGate = "UI_jianzhu_01_b";
        const string aloGate = "UI_jianzhu_02_b";
        const string saoTower = "UI_jianzhu_01_a";
        const string aloTower = "UI_jianzhu_02_a";

        public void SetItem(UFStrengthBuildingInfo item, NPCUIBuildingPage page)
        {
            curLevel = item.level;
            buildingLevel.text = TableTextBlo.GetText("level_colon") + curLevel;
            maxLevel = item.maxLevel;
            type = item.type;
            costNum = (int)item.upgradeCost.resourceNum;
            cost.text = costNum.ToString();
            //string[] _Str = item.upgradeDesc.Split(',');
            //additionType.text = _Str[0];
            //addition.text = _Str[1];
            addition.text = item.upgradeDesc;
            this.page = page;
            SetName(type);
        }

        void SetName(int type)
        {
            int camp = UIUnionFightRunMap.GetNetData().charactorManager.GetSelfCharactor().Camp;
            switch(type)
            {
                case 0:
                    building.spriteName = camp == 0 ? saoChariot : aloChariot;
                    buildingName.text = TableTextBlo.GetText("union_fight_chariot");
                    break;
                case 1:
                    building.spriteName = camp == 0 ? saoGate : aloGate;
                    buildingName.text = TableTextBlo.GetText("union_fight_gate");
                    break;
                case 2:
                    building.spriteName = camp == 0 ? saoTower : aloTower;
                    buildingName.text = TableTextBlo.GetText("union_fight_tower");
                    break;

            }
        }

        public void LevelUp()
        {
            if (curLevel >= maxLevel)
            {
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("union_fight_strengthen_max_level"));
                return;
            }
            int ownResNum = UIUnionFightRunMap.GetNetData().netBattle.GetSelfCampUnionResourseNum();
            if (ownResNum < costNum)
            {
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("field_res_not_enough"));
                return;
            }
            if (page.Timer > 0)
            {
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("union_fight_strengthen_cdTime_limit"));
                return;
            }
            UIUnionFightRunMap.GetNetData().SendCSUFStrengthBuilding(type,page.dictData.UFNId+"");
        }
    }
}
